<?php
/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
require_once realpath(dirname(__FILE__)).'/../osapi/osapi.php';
class FCUtils {
  /**
   * 
   * @param string $fcauth_token
   * @return osapiPerson viewer
   */
  public static function getViewer($fcauth_token) {
    $viewer =null;
    $provider = new osapiFriendConnectProvider();
    $auth = new osapiFCAuth($fcauth_token);
    $osapi = new osapi($provider,$auth);
    $osapi->setStrictMode(true);
    $batch = $osapi->newBatch();
    $profile_fields = array('id' , 'name', 'thumbnailUrl', 'profileUrl');
    $self_request_params = array(
      'userId' => '@viewer',            // Person we are fetching.
      'groupId' => '@self',             // @self for one person.
      'fields' => $profile_fields // Which profile fields to request.
    );
    $batch->add($osapi->people->get($self_request_params), 'viewer');
    try {
      $a = $batch->execute();
      $viewer = $a['viewer'];
    } catch (Exception $e) {
      // log error
    }
    return $viewer; 
  }
  /**
   * 
   * @param int $site_id
   * @return string fcauth token
   */
  public static function getFCAuthToken($site_id) {
    $site_id = trim($site_id);
    $key = "fcauth{$site_id}";
    return isset($_COOKIE[$key]) ? $_COOKIE[$key] : false;
  }
}